<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      size="small"
    >
      <el-form-item label="选手" prop="playerName">
        <el-input
          v-model="queryParams.playerName"
          placeholder="请输入选手名称"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
          size="small"
        >
          <el-option label="存活" :value="1" />
          <el-option label="已淘汰" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >查询</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <!-- 操作按钮区域 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-upload
          class="upload-demo"
          :action="uploadUrl"
          :show-file-list="false"
          :on-success="handleImportSuccess"
          :before-upload="beforeImportUpload"
        >
          <el-button type="primary" icon="el-icon-upload2" size="mini"
            >导入Excel</el-button
          >
        </el-upload>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-download"
          size="mini"
          @click="handleExportTemplate"
          >下载模板</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <!-- 表格区域 -->
    <el-table v-loading="loading" :data="playerList" style="width: 100%">
      <el-table-column label="头像" align="center" width="80">
        <template slot-scope="scope">
          <el-avatar
            :size="40"
            :src="scope.row.avatar"
            :default-src="require('@/assets/images/default-avatar.png')"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="昵称"
        align="center"
        prop="name"
        min-width="120"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="性别" align="center" prop="gender" width="80">
        <template slot-scope="scope">
          {{ scope.row.gender === 1 ? "男" : "女" }}
        </template>
      </el-table-column>
      <el-table-column
        label="ID"
        align="center"
        prop="id"
        min-width="80"
        sortable
      />
      <el-table-column
        label="选手名"
        align="center"
        prop="name"
        min-width="120"
        :show-overflow-tooltip="true"
        sortable
      />
      <el-table-column
        label="状态"
        align="center"
        prop="status"
        min-width="80"
        sortable
      >
        <template slot-scope="scope">
          <span>{{ scope.row.status === 1 ? "存活" : "已淘汰" }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="粉丝数量"
        align="center"
        prop="fansCount"
        min-width="100"
        sortable
      />
      <el-table-column
        label="热力值"
        align="center"
        prop="thermalValue"
        min-width="100"
        sortable
      >
        <template slot-scope="scope">
          <span>{{ scope.row.thermalValue || "待计算" }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="淘汰值"
        align="center"
        prop="expireValue"
        min-width="100"
        sortable
      >
        <template slot-scope="scope">
          <span>{{ scope.row.expireValue || "待计算" }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="自定义值"
        align="center"
        prop="customizeValue"
        min-width="100"
        sortable
      />
      <el-table-column
        label="点赞数"
        align="center"
        prop="likeCount"
        min-width="80"
        sortable
      />
      <el-table-column
        label="倒赞数"
        align="center"
        prop="dislikeCount"
        min-width="80"
        sortable
      />
      <el-table-column
        label="评论数"
        align="center"
        prop="commentsCount"
        min-width="80"
        sortable
      />
      <el-table-column
        label="动态点赞数"
        align="center"
        prop="tweetsLikeCount"
        min-width="100"
        sortable
      />
      <el-table-column
        label="动态评论数"
        align="center"
        prop="tweetsCommentsCount"
        min-width="100"
        sortable
      />
      <el-table-column
        label="聊天提及数"
        align="center"
        prop="chatContainsCount"
        min-width="100"
        sortable
      />
      <el-table-column
        label="人气值"
        align="center"
        prop="popularityValue"
        min-width="100"
        sortable
      />
      <el-table-column
        label="关注时长"
        align="center"
        prop="focusTime"
        min-width="100"
        sortable
      />
      <el-table-column
        label="更新时间"
        align="center"
        prop="updateTime"
        min-width="160"
        sortable
      />
      <el-table-column label="操作" align="center" width="150" fixed="right">
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="handleEdit(scope.row)"
            >修改</el-button
          >
          <el-button
            type="text"
            size="mini"
            @click="handleEliminate(scope.row)"
            v-if="scope.row.status === 1"
            >淘汰</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <edit-player
      ref="editPlayer"
      :visible.sync="editDialogVisible"
      :title="editDialogTitle"
      @success="handleEditSuccess"
    />
  </div>
</template>

<script>
import { listPlayer, eliminatePlayer, downloadTemplate } from "@/api/player";
import EditPlayer from "./components/editPlayer.vue";
import Pagination from "@/components/Pagination";

export default {
  name: "PlayerManagement",
  components: {
    EditPlayer,
    Pagination,
  },
  data() {
    return {
      uploadUrl: process.env.VUE_APP_BASE_API + "/player/importData",
      loading: false,
      showSearch: true,
      total: 0,
      playerList: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        playerName: undefined,
        status: undefined,
      },
      editDialogVisible: false,
      editDialogTitle: "新增选手",
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 获取列表数据
    getList() {
      this.loading = true;
      listPlayer(this.queryParams)
        .then((res) => {
          console.log(res);
          this.playerList = res.rows;
          this.total = res.total;
        })
        .catch((error) => {
          console.error("获取列表失败:", error);
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 查询按钮操作
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 重置按钮操作
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 新增按钮操作
    handleAdd() {
      this.editDialogTitle = "新增选手";
      this.editDialogVisible = true;
    },
    // 修改按钮操作
    handleEdit(row) {
      this.editDialogTitle = "修改选手";
      this.editDialogVisible = true;
      this.$nextTick(() => {
        if (this.$refs.editPlayer) {
          const formData = {
            id: row.id,
            name: row.name,
            status: row.status,
            deviceId: row.deviceId,
            phoneNum: row.phoneNum,
            wechatId: row.wechatId,
            fansCount: row.fansCount,
            expireValue: row.expireValue,
            customizeValue: row.customizeValue,
          };
          this.$refs.editPlayer.setFormData(formData);
        }
      });
    },
    // 淘汰选手按钮操作
    handleEliminate(row) {
      this.$modal
        .confirm("确认要淘汰该选手吗？")
        .then(() => {
          eliminatePlayer(row.id)
            .then((res) => {
              this.$modal.msgSuccess("淘汰成功");
              this.getList();
            })
            .catch((err) => {
              console.error("淘汰失败:", err);
            });
        })
        .catch(() => {});
    },
    // 编辑成功回调
    handleEditSuccess() {
      this.getList();
    },
    // 上传自定义值按钮操作
    handleUpload() {
      this.$modal.msgWarning("功能开发中...");
    },
    // 臂环显示按钮操作
    handleTimeUpdate() {
      this.$modal.msgWarning("功能开发中...");
    },
    handleImportSuccess(response) {
      if (response.code === 200) {
        this.$modal.msgSuccess("导入成功");
        this.getList();
      } else {
        this.$modal.msgError(response.msg || "导入失败");
      }
    },
    beforeImportUpload(file) {
      const isExcel =
        file.type ===
          "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ||
        file.type === "application/vnd.ms-excel";
      if (!isExcel) {
        this.$message.error("只能上传Excel文件!");
        return false;
      }
      return true;
    },
    handleExportTemplate() {
      downloadTemplate().then((response) => {
        const blob = new Blob([response.data]);
        const link = document.createElement("a");
        link.href = window.URL.createObjectURL(blob);
        link.download = "红人导入模板.xlsx";
        link.click();
      });
    },
  },
};
</script>

<style scoped>
.app-container {
  padding: 20px;
}
.mb8 {
  margin-bottom: 8px;
}
</style>
